from typing import List, Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


    def preorder_traversal(self) -> List[int]:
        result = []
        def dfs(node):
            if not node:
                return
            result.append(node.val)
            dfs(node.left)
            dfs(node.right)
        dfs(self)
        return result


def build_tree_node(nums:List[int])->Optional[TreeNode]:
    if len(nums)==0:
        return None
    root=TreeNode(nums[0])
    
    # 构建TreeNode
    queue = [root]
    i = 1
    while i < len(nums):
        current = queue.pop(0)
        if nums[i] is not None:
            current.left = TreeNode(nums[i])
            queue.append(current.left)
        i += 1
        if i < len(nums) and nums[i] is not None:
            current.right = TreeNode(nums[i])
            queue.append(current.right)
        i += 1



    return root

if __name__=='__main__':
    nums=[1,None,2,3]
    tn=build_tree_node(nums)
    result=tn.preorder_traversal()
    print(result)
    pass